﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using bdnt_pets_system.Tools;
using bdnt_pets_system_Entity;
using System.Collections.ObjectModel;
using bdnt_pets_system.DataServiceReference;

namespace bdnt_pets_system.PageViews.FailuresTraking
{
    public partial class NewTrakingControl : BaseUserControl
    {
        DataServiceReference.DataServiceClient dataClient;
        FailureServiceReference.FailureServiceClient faiClient;
        int pageSize = SessionManager.PageSize;
        int currentPage = 1;

        public NewTrakingControl()
        {
            InitializeComponent();
            dataClient = new DataServiceReference.DataServiceClient();
            faiClient = new FailureServiceReference.FailureServiceClient();
            InitData();
        }

        void InitData()
        {
            dpTrakingDate.SelectedDate = DateTime.Now;
            InitEvent(); 
        }

        void InitEvent()
        {
            hbXztd.Click += new RoutedEventHandler(hbXztd_Click);
            dataClient.GetPartListByConditionCompleted += new EventHandler<GetPartListByConditionCompletedEventArgs>(dataClient_GetPartListByConditionCompleted);
            dataClient.GetPartListByConditionCountCompleted += new EventHandler<GetPartListByConditionCountCompletedEventArgs>(dataClient_GetPartListByConditionCountCompleted);           
            failurePageC.dpPage.PageIndexChanged += new EventHandler<EventArgs>(dpPage_PageIndexChanged);
            dataClient.GetPartListByConditionCountAsync(null);
        }

        public int zzTdId;
        public List<int> selectMemembers = new List<int>();

        void hbXztd_Click(object sender, RoutedEventArgs e)
        {
            AddToTrakingWindow atw = new AddToTrakingWindow();
            atw.Show();
            atw.Closed += new EventHandler(atw_Closed);
        }

        void atw_Closed(object sender, EventArgs e)
        {
            var cuo = sender as AddToTrakingWindow;
            if (cuo.DialogResult == true)
            {
                zzTdId = cuo.zzTdId;
                selectMemembers = cuo.selectMemembers;
                tbZrr.Text = cuo.selectMembersName;
                tbTd.Text = cuo.zzTdName;
            }
        }
         
        void dpPage_PageIndexChanged(object sender, EventArgs e)
        {
            currentPage = failurePageC.PageIndex + 1;
            dataClient.GetPartListByConditionAsync(null, pageSize, currentPage); 
        }

        void dataClient_GetPartListByConditionCountCompleted(object sender, GetPartListByConditionCountCompletedEventArgs e)
        {
            failurePageC.LoadPages(e.Result, pageSize);
        }
        ObservableCollection<Tbl_Part> cuConditionParts;
        void dataClient_GetPartListByConditionCompleted(object sender, GetPartListByConditionCompletedEventArgs e)
        {
            cuConditionParts = e.Result;
            LoadParts();
        }
         
        Tbl_Part cuPartInfo;
        void LoadParts()
        {
            wpParts.Children.Clear();
            if (cuConditionParts == null || cuConditionParts.Count() < 1)
                return;
            foreach (var item in cuConditionParts)
            {
                Border bd = new Border();
                HyperlinkButton hb = new HyperlinkButton() { Tag = item };
                if (cuPartInfo != null && cuPartInfo.PartNo == item.PartNo)
                {
                    bd.BorderBrush = new SolidColorBrush(Colors.Red);
                    //bd.Background = new SolidColorBrush(Colors.Gray);
                    bd.Background = new SolidColorBrush(HelpTools.HtmlToColor("#D0D7E3"));
                }
                hb.Click += new RoutedEventHandler(hb_Click);
                TextBlock tb = new TextBlock()
                {
                    Margin = new Thickness(5),
                    TextWrapping = TextWrapping.Wrap
                };

                tb.Text = SessionManager.IsCN_ZH ? item.PartName + "(" + item.en_PartName + ")" : item.en_PartName;
                hb.Content = tb;
                bd.Child = hb;
                wpParts.Children.Add(bd);
            }
        }
        public SaveTrakingControl sfC;
        void hb_Click(object sender, RoutedEventArgs e)
        {
            var cuo = (sender as HyperlinkButton);
            cuPartInfo = cuo.Tag as Tbl_Part;
            if (sfC == null)
            {
                //sfC = new SaveTrakingControl(cuPartInfo);
                sfC.Tag = this;
                gdMC.Children.Add(sfC);
            }
            else
            {
                //if (sfC.CuPartInfo != null)
                //{
                //    if (cuPartInfo.PartNo != sfC.CuPartInfo.PartNo)
                //    {
                //        sfC.wpFailures.Children.Clear();
                //        //sfC.wpPositions.Children.Clear();
                //        //sfC.selectPositionInfo = null;
                //        sfC.selectFailure = null;
                //        sfC.CuPartInfo = cuPartInfo;
                //        //sfC.QueryData();
                //    }
                //}
            }
            sfC.Visibility = System.Windows.Visibility.Visible;
            gdParts.Visibility = System.Windows.Visibility.Collapsed;
            LoadParts();
        }          
    }
}
